package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.redis.service.ITravelRedisService;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
	@Autowired
	private TravelCommentRepository commentRepository;
	@Autowired
	private ITravelRedisService travelRedisService;
	@Override
	public void addComment(TravelComment travelComment) {
		travelComment.setId(null);
		// 获取该评论关联的评论
		TravelComment refComment = travelComment.getRefComment();
		// 判断是否拥有关联的评论
		if(StringUtils.hasLength(refComment.getId())){
			// 拥有就设置到该评论中
			travelComment.setRefComment(this.get(refComment.getId()));
		}
		// 设置发送时间
		travelComment.setCreateTime(new Date());
		travelRedisService.replynumIncreas(travelComment.getTravelId());
		commentRepository.save(travelComment);
	}

	@Override
	public void update(TravelComment travelComment) {
		commentRepository.save(travelComment);
	}

	@Override
	public List<TravelComment> comments(String travelId) {

		return commentRepository.findByTravelIdOrderByCreateTimeDesc(travelId);
	}

	@Override
	public TravelComment get(String id) {
		if(StringUtils.hasLength(id)){
			return commentRepository.findById(id).orElse(null);
		}
		return null;
	}
}
